package nc.bs.hrhi.bp;

import java.util.HashMap;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.pub.BusinessException;

public abstract class AbsPubOp {
	
	private static Map<String, Object> params = null;
	private BaseDAO dao;
	private Map<String, Object> cache = new HashMap<String, Object>();

	static {
		if(params == null) {
			params = new HashMap<String, Object>();
			params.put("psnjob_condition", " hi_psnjob.lastflag='Y' and hi_psnjob.recordnum=0 and hi_psnjob.endflag='N' and hi_psnjob.ismainjob='Y' ");
			params.put("ctrt_condition", " hi_psndoc_ctrt.lastflag='Y' and hi_psndoc_ctrt.recordnum=0 ");
		}
	}
	
	private boolean containsKey(String key) {
		return cache.containsKey(key);
	}
	private void put(String key, Object value) {
		cache.put(key, value);
	}
	
	private Object get(String key) {
		return cache.get(key);
	}
	
	private Map<String, Object> mapNullFill(Map<String, Object> map) {
		if(map == null) {
			map = new HashMap<String, Object>();
		}
		if(null == map.get("pk")) {
			map.put("pk", "");
		}
		if(null == map.get("code")) {
			map.put("code", "");
		}
		if(null == map.get("name")) {
			map.put("name", "");
		}
		if(null == map.get("def1")) {
			map.put("def1", "");
		}
		if(null == map.get("def2")) {
			map.put("def2", "");
		}
		if(null == map.get("def3")) {
			map.put("def3", "");
		}
		if(null == map.get("def4")) {
			map.put("def4", "");
		}
		
		return map;
	}
	
	private String stringNullFill(String content){
		if(content == null) {
			return "";
		}else {
			return content;
		}
	}
	
	public BaseDAO getDao() {
		if(this.dao == null) this.dao = new BaseDAO();
		return dao;
	}
	
	public String getJobContidion() {
		return (String) params.get("psnjob_condition");
	}
	public String getCtrtContidion() {
		return (String) params.get("ctrt_condition");
	}
	
	public String getColvalue(String tablename, String showfieldname, String primkeyname, String pk) {

		String key = tablename + showfieldname + primkeyname + pk;
		if(containsKey(key)) {
			return (String) get(key);
		}
		
		String sql = "select "+showfieldname+" from "+tablename+" where dr=0 and "+primkeyname+"='"+pk+"'";
		
		String value = null;
		
		try {
			value = (String) getDao().executeQuery(sql, new ColumnProcessor());
		} catch (DAOException e) {
			e.printStackTrace();
		}
		value = stringNullFill(value);
		put(key, value);
		
		return value;
		
	}
	
	public String getString(String viewName, String pk_psndoc) throws DAOException {

		String key = viewName + pk_psndoc;
		if(containsKey(key)) {
			return (String)get(key);
		}
		
		String sql = "select * from " + viewName + " where pk_psndoc='"+pk_psndoc+"'";
		
		String value = (String) getDao().executeQuery(sql, new ColumnProcessor());
		
		value = stringNullFill(value);
		
		put(key, value);
		
		return value;
	}
	
	public Map<String, Object> getDoc(String viewName, String pk_psndoc) throws BusinessException {
		String key = viewName + pk_psndoc;
		if(containsKey(key)) {
			return (Map<String, Object>)get(key);
		}
		
		String sql = "select * from " + viewName + " where pk_psndoc='"+pk_psndoc+"'";
		
		Map<String, Object> value = (Map<String, Object>) getDao().executeQuery(sql, new MapProcessor());
		value = mapNullFill(value);
		put(key, value);
		
		return value;
	}
}
